package com.oa.util;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oa.entity.Power;
import com.oa.entity.User;
@Component
public class InitData {
	@Resource
	private SessionFactory sessionFactory;
	

	@Transactional
	public void initURL() {
		Session session = sessionFactory.openSession();
		Transaction t = session.beginTransaction();
		// ==============================================================
		// 保存管理员
		t.begin();
		User user = new User();
		user.setUser_loginName("admin");
		user.setUser_name("管理员");
		user.setUser_password(DataUtil.md5("123456"));
		session.save(user);

		// ==============================================================
		Power menu, menu1, menu2, menu3, menu4, menu5;
		
		// 个人信息
		menu = new Power("个人办公", null, null);
		menu1 = new Power("我的任务", "/postAction_list", menu);
		menu2 = new Power("我的便签", "/usermarkAction_list", menu);
		menu3 = new Power("通讯录", "/linkAction_list", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		

		// --------系统模块------------
		menu = new Power("系统管理", null, null);
		menu1 = new Power("岗位管理", "/postAction_list", menu);
		menu2 = new Power("部门管理", "/departmentAction_list", menu);
		menu3 = new Power("用户管理", "/userAction_list", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);

		session.save(new Power("岗位列表", "/postAction_list", menu1));
		session.save(new Power("岗位删除", "/postAction_delete", menu1));
		session.save(new Power("岗位添加", "/postAction_add", menu1));
		session.save(new Power("岗位修改", "/postAction_update", menu1));
		session.save(new Power("设置权限", "/postAction_setPowerPage", menu1));

		session.save(new Power("部门列表", "/departmentAction_list", menu2));
		session.save(new Power("部门删除", "/departmentAction_delete", menu2));
		session.save(new Power("部门添加", "/departmentAction_add", menu2));
		session.save(new Power("部门修改", "/departmentAction_update", menu2));

		session.save(new Power("用户列表", "/userAction_list", menu3));
		session.save(new Power("用户删除", "/userAction_delete", menu3));
		session.save(new Power("用户添加", "/userAction_add", menu3));
		session.save(new Power("用户修改", "/userAction_update", menu3));
		session.save(new Power("初始化密码", "/userAction_initPassword", menu3));

		// ---------论坛模块-----------
		menu = new Power("论坛模块", null, null);
		menu1 = new Power("板块管理", "/forum_queryAllForum.action?type=1", menu);
		menu2 = new Power("板块列表", "/forum_queryAllForum.action?type=0", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		
		session.save(new Power("移动到其他板块","/forum_queryForums.action",menu2));
		session.save(new Power("删除","/topic_deleteTopic.action",menu2));

		// ---------工作流程-----------
		menu = new Power("工作流程", null, null);
		menu1 = new Power("流程管理", "/liuChengDingYi_list", menu);
		menu2 = new Power("模板管理", "/templateAction_list", menu);
		
		menu3 = new Power("起草申请", "/applicationAction_list", menu);
		menu4 = new Power("待我审批", "/applicationAction_myTaskList", menu);
		menu5 = new Power("我的申请", "/applicationAction_myApplicationList", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		session.save(menu5);
		
		
		session.save(new Power("部署流程定义","/liuChengDingYi_addPage",menu1));
		session.save(new Power("删除流程定义","/liuChengDingYi_delete",menu1));
		session.save(new Power("查看流程图","/liuChengDingYi_look",menu1));
		
		session.save(new Power("模板列表","/templateAction_list",menu2));
		session.save(new Power("新增模板","/templateAction_addPage",menu2));
		session.save(new Power("删除模板","/templateAction_delete",menu2));
		session.save(new Power("修改模板","/templateAction_update",menu2));
		session.save(new Power("下载模板","/templateAction_upload",menu2));
		
		
		session.save(new Power("审批处理","/applicationAction_flowApprove",menu4));
		session.save(new Power("流程记录","/applicationAction_flowApprovedHistory",menu4));
		
		t.commit();
		
		
	}
	public static void main(String[] args) {
		ApplicationContext act = new ClassPathXmlApplicationContext("spring.xml");
		act.getBean("initData",InitData.class).initURL();
	}

	
}
